from flask import Flask as _Flask,jsonify
from flask import request
from flask import render_template
from flask.json import JSONEncoder as _JSONEncoder
from jieba.analyse import extract_tags
import decimal
import utils
import news
from urllib.parse import urlencode
import string
import scraper
import numpy as np
class JSONEncoder(_JSONEncoder):
        def default(self, o):
            if isinstance(o, decimal.Decimal):
                return float(o)
            super(_JSONEncoder, self).default(o)

class Flask(_Flask):
    json_encoder = JSONEncoder


app = Flask(__name__)

@app.route('/')
def hello_word3():
    return render_template("main.html")


@app.route('/time')
def get_time():
    return utils.get_time()

@app.route('/get_c1_data')
def get_c1_data():
    data = utils.get_c1_data()
    # return jsonify({"confirm":data[1],"suspect":data[2],"heal":data[3],"dead":data[4],''})
    returnjson = {
        'total':data[1],
        'input':data[2],
        'zhiyu':data[3],
        'dead':data[4],
        'yisi':data[5],
        'wuzz':data[6],
        'nowtotal':data[7],
        'serious':data[8],
    }
    return jsonify(returnjson)

@app.route('/get_center_data')
def get_center_data():
    data = utils.get_center_data()
    json = []
    for i in data:
        jsonData = {
            'name':i[1],
            'value':i[2]
        }
        json.append(jsonData)
    return jsonify(json)

@app.route('/get_center_data_now')
def get_center_data_now():
    data = utils.get_center_data()
    json = []
    for i in data:
        jsonData = {
            'name':i[1],
            'value':i[6]
        }
        json.append(jsonData)
    return jsonify(json)


@app.route('/c2')
def get_c2_data():
    res = []
    for tup in utils.get_c2_data():
        res.append({"name":tup[0],"value":int(tup[1])})
    return jsonify({"data":res})

@app.route('/get_l1_data')
def get_l1_data():
    data = utils.get_l1_data()
    day,confirm,suspect,heal,dead = [],[],[],[],[]
    for i in range(0, data.__len__())[::-1]:
        day.append(data[i][5])
        confirm.append(data[i][1])
        suspect.append(data[i][2])
        heal.append(data[i][3])
        dead.append(data[i][4])
    return jsonify({"day":day,"confirm":confirm,"suspect":suspect,"heal":heal,"dead":dead})

@app.route('/get_l2_data')
def get_l2_data():
    data = utils.get_l2_data()
    day,confirm_add,suspect_add = [],[],[]
    for i in range(0, data.__len__())[::-1]:
        day.append(data[i][3])
        confirm_add.append(data[i][1])
        suspect_add.append(data[i][2])
    return jsonify({"day":day,"confirm_add":confirm_add,"suspect_add":suspect_add})


@app.route('/get_r1_data')
def get_r1_data():
    data = utils.get_r1_data()
    city,confirm = [],[]
    for i in data:
        city.append(i[1])
        confirm.append(i[2])
    return jsonify({"city": city,"confirm": confirm})

@app.route('/get_r2_data')
def get_r2_data():
    data = utils.get_r2_data()
    city,confirm = [],[]
    for i in data:
        city.append(i[1])
        confirm.append(i[6])
    return jsonify({"city": city,"confirm": confirm})

# @app.route('/get_r2_data')
# def get_r2_data():
#     data = utils.get_r2_data()
#     return jsonify(data)

@app.route('/get_news_data/<area>')
def get_news_data(area):
    area = urlencode({'query':area+'新型肺炎最新动态'})
    data = news.spliderNews(area)
    return jsonify(data)
@app.route('/splider')
def splider():
    scraper.splider()
    news.get_total()
    return 123

if __name__ == '__main__':
    app.run(host="127.0.0.1",debug=True)
